package business;

import android.content.Context;
import android.util.Log;


import com.loopj.android.http.RequestParams;

import org.json.JSONException;
import org.json.JSONObject;



import common.Constants;
import model.Threshold;
import shuntianfu.com.shuntianfucompressor.R;
import utils.CheckNetUtil;
import utils.DataHandler;
import utils.HttpRequestUtil;
import utils.HttpServiceHandler;
import utils.SharedPreferencesUtils;

/**
 * Created by haopei on 2016/3/31.
 */
public class CompressorDataManager {

    private static final String TAG = "CompressorDataManager";

    private CompressorDataManager() {
    }

    static class CompreesorDatamanagerHolder {

        static CompressorDataManager compressorDataManager = new CompressorDataManager();
    }

    public static CompressorDataManager getInstance() {
        return CompreesorDatamanagerHolder.compressorDataManager;
    }

    /**
     * 根据压缩机编号请求阀值
     *
     * @param context
     * @param compressCode
     * @param dataHandler
     */
    public void requstFazhi(final Context context, String compressCode, final DataHandler dataHandler) {
        if (!CheckNetUtil.checkNetWork(context)) {
            if (dataHandler != null) {
                dataHandler.onData(Constants.Error.NETWORK_IS_UNREACHABLE, context.getString(R.string.error_net_unconnect), null);
            }
            return;
        }

        String url = Constants.SERVER_HOST + Constants.QUERY_THRESHOLD + "/" + compressCode;
        utils.Log.e(TAG,url);
        HttpRequestUtil.sendSimpleRequestAsyncGet(url, context, dataHandler);

    }

    /**
     * 更新阀值
     *
     * @param context
     * @param threshold
     * @param dataHandler
     */
    public void updateFazhi(final Context context, Threshold threshold, final DataHandler dataHandler) {

        if (!CheckNetUtil.checkNetWork(context)) {
            if (dataHandler != null) {
                dataHandler.onData(Constants.Error.NETWORK_IS_UNREACHABLE, context.getString(R.string.error_net_unconnect), null);
            }
            return;
        }

        HttpServiceHandler httpServiceHandler = new HttpServiceHandler(dataHandler) {

            @Override
            public void onResponse(int code, String reason, Object msgData) {
                if (code == Constants.SERVER_SUCCESS) {
                    Log.d(TAG, "msgData=" + msgData.toString());
                    dataHandler.onData(code, reason, msgData);
                } else {
                    if (null != getDataHandler()) {
                        getDataHandler().onData(code, reason, msgData);
                    }
                }
            }

            @Override
            public void onError(int code, String reason, Throwable e) {
                super.onError(code, reason, e);
                if (null != getDataHandler()) {
                    getDataHandler().onData(code, reason, null);
                }
            }
        };
        JSONObject jsonObject = new JSONObject();
        RequestParams params = new RequestParams();

        try {
//            jsonObject.put("id", threshold.getId());
            jsonObject.put("dianyaUpper", threshold.getDianyaUpper());
            jsonObject.put("dianliuUpper", threshold.getDianliuUpper());
            jsonObject.put("wenduUpper", threshold.getWenduUpper());
            jsonObject.put("gaoyaUpper", threshold.getGaoyaUpper());
            jsonObject.put("diyaUpper", threshold.getDiyaUpper());

            jsonObject.put("dianyaLower", threshold.getDianyaLower());
            jsonObject.put("dianliuLower", threshold.getDianliuLower());
            jsonObject.put("wenduLower", threshold.getWenduLower());
            jsonObject.put("gaoyaLower", threshold.getGaoyaLower());
            jsonObject.put("diyaLower", threshold.getDiyaLower());
            jsonObject.put("compressorName", threshold.getCompressorName());

        } catch (JSONException e) {
            e.printStackTrace();
        }

        Log.i(TAG, "url==" + Constants.SERVER_HOST
                + Constants.UPDATE_THRESHOLD);
        HttpRequestUtil.sendSimpleRequestAsync(Constants.SERVER_HOST
                + Constants.UPDATE_THRESHOLD, jsonObject, context, dataHandler);


//		String token = SharedPreferencesUtils.getString(context, Constants.USER_TOKEN, "");
//		Log.i(TAG, "token==" + token);
//		Log.d(TAG, "请求 threshold=" + threshold.toString());
//		String url = Constants.SERVER_HOST + Constants.UPDATE_THRESHOLD
//				+ "?dianyaThreshold=" + threshold.getDianyaThreshold()
//				+ "&dianliuThreshold=" + threshold.getDianliuThreshold()
//				+ "&wenduThreshold=" + threshold.getWenduThreshold()
//				+ "&gaoyaThreshold=" + threshold.getGaoyaThreshold()
//				+ "&diyaThreshold=" + threshold.getDiyaThreshold()
//				+ "&token="+token;
//
//		HttpRequestUtil.sendSimpleRequestAsyncGet(url, context, dataHandler);

    }
}
